package com.cuiweiyou.vigilesmicrecord.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cuiweiyou.vigilesmicrecord.R;
import com.cuiweiyou.vigilesmicrecord.base.BaseActivity;
import com.cuiweiyou.vigilesmicrecord.base.ContantValue;
import com.cuiweiyou.vigilesmicrecord.ui.adapter.SoundListAdapter;
import com.cuiweiyou.vigilesmicrecord.util.PlayUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SoundsActivity extends BaseActivity {

    @BindView(R.id.soundPath)
    TextView soundPath;

    @BindView(R.id.emptyView)
    View emptyView;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    List<String> soundPathList;

    SoundListAdapter wavAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound_list);

        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.exitImageView)
    public void onClickExitView() {
        finish();
    }

    private void initView() {
        soundPath.setText(ContantValue.getSoundSavePath());

        wavAdapter = new SoundListAdapter(soundPathList, playClickListener, nameClickListener, nameLongClickListener);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(wavAdapter);
    }

    private void initData() {
        String path = ContantValue.getSoundSavePath();
        File f = new File(path);
        if (f.exists()) {
            soundPathList = new ArrayList<>();
            File[] files = f.listFiles();
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                String fp = file.getAbsolutePath();
                soundPathList.add(fp);
            }
            wavAdapter.setData(soundPathList);
        }
    }

    SoundListAdapter.OnPlayClickListener playClickListener = new SoundListAdapter.OnPlayClickListener() {
        @Override
        public void onPlay(String path) {
            if (null == path) {
                Toast.makeText(SoundsActivity.this, "无效的录音文件", Toast.LENGTH_SHORT).show();
                return;
            }

            File f = new File(path);
            if (!f.exists()) {
                Toast.makeText(SoundsActivity.this, "没有找到录音文件", Toast.LENGTH_SHORT).show();
                return;
            }

            boolean playing = PlayUtil.getInstance().isPlaying();
            if (playing) {
                PlayUtil.getInstance().stopPlay();
            }

            PlayUtil.getInstance().startPlay(path);
        }
    };

    SoundListAdapter.OnNameClickListener nameClickListener = new SoundListAdapter.OnNameClickListener() {
        @Override
        public void onName(int index, String path) {
            if (null == path) {
                return;
            }

            File f = new File(path);
            if (!f.exists()) {
                return;
            }

            boolean playing = PlayUtil.getInstance().isPlaying();
            if (playing) {
                PlayUtil.getInstance().stopPlay();
            }

            confirmDelete(index, f);
        }
    };

    SoundListAdapter.OnNameLongClickListener nameLongClickListener = new SoundListAdapter.OnNameLongClickListener() {
        @Override
        public void onNameLongClick(int index, String path) {
            if (null == path) {
                return;
            }

            File f = new File(path);
            if (!f.exists()) {
                return;
            }

            boolean playing = PlayUtil.getInstance().isPlaying();
            if (playing) {
                PlayUtil.getInstance().stopPlay();
            }

            confirmChangeName(index, f);
        }
    };

    private void confirmDelete(final int index, final File f) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("删除此录音");

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                f.delete();
                soundPathList.remove(index);
                wavAdapter.setData(soundPathList);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void confirmChangeName(final int index, final File f) {

        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        View view = View.inflate(this, R.layout.view_change_name, null);

        final EditText editText = (EditText) view.findViewById(R.id.editText);
        final TextView okTextView = (TextView) view.findViewById(R.id.okTextView);
        final TextView noTextView = (TextView) view.findViewById(R.id.noTextView);
        okTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                editText.setEnabled(false);
                okTextView.setEnabled(false);
                noTextView.setEnabled(false);

                boolean b = changeName(editText.getText().toString(), index, f);
                if (!b) {
                    editText.setEnabled(true);
                    okTextView.setEnabled(true);
                    noTextView.setEnabled(true);
                    Toast.makeText(SoundsActivity.this, "请输入有效名称", Toast.LENGTH_SHORT).show();
                    return;
                }

                dialog.dismiss();
            }
        });

        noTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.setView(view);
        dialog.show();
    }

    private boolean changeName(String target, int index, File src) {
        target = target.trim();

        boolean empty = TextUtils.isEmpty(target);
        if (empty) {
            return false;
        }

        File parent = src.getParentFile();
        String parentPath = parent.getAbsolutePath();

        String tf = parentPath + "/" + target + ".wav";

        boolean b = src.renameTo(new File(tf));

        if (b) {
            Toast.makeText(SoundsActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
            soundPathList.clear();
            initData();
        }

        return true;
    }
}


